<?php

namespace App\Http\Controllers\Network;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkGoodsOrderModel;
use Illuminate\Http\Request;

class ShopController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  店铺信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function ShopDetails(Request $request)
    {
       
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();
        $avatar_str = mb_substr($user_data['avatar'],0,4);

        if($avatar_str != 'http'){
            $user_data['avatar'] = env('OSS_URL').$user_data['avatar'];
        }

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$user_data]);

    }

    /**
     * Get a JWT via given credentials.
     *  店铺设置
     * @return \Illuminate\Http\JsonResponse
     */
    public function ShopSetup(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $name = $request->post('name');

        if(empty($name)){
            return response()->json(['code' => '203', 'msg' => '店铺名称不能为空']);
        }

        $mobile = $request->post('mobile');

        if(empty($mobile)){
            return response()->json(['code' => '203', 'msg' => '店铺电话不能为空']);
        }

        $distribution_scope = $request->post('distribution_scope');

        if(empty($distribution_scope)){
            return response()->json(['code' => '203', 'msg' => '配送范围不能为空']);
        }

        $distribution_time = $request->post('distribution_time');

        if(empty($distribution_time)){
            return response()->json(['code' => '203', 'msg' => '配送时间不能为空']);
        }

        $address = $request->post('address');

        if(empty($address)){
            return response()->json(['code' => '203', 'msg' => '地址不能为空']);
        }

        AdminUserModel::where(['openid' => $openid])->update([
            'name'=>$name,
            'mobile'=>$mobile,
            'distribution_scope'=>$distribution_scope,
            'distribution_time'=>$distribution_time,
            'address'=>$address
        ]);

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }

    /**
     * Get a JWT via given credentials.
     *  我的
     * @return \Illuminate\Http\JsonResponse
     */
    public function ShopMy(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
        $user_data['distribution_order_num'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->count();
        $user_data['loading_order_num'] = NetworkGoodsOrderModel::where(['network_id'=>$user_data['id']])->count();
        $user_data['balance_delivery_fee'] = $user_data['distribution_price'];
        $user_data['barrel_deposit_balance'] = NetworkGoodsModel::where(['network_id'=>$user_data['id']])->sum('barrel_deposit');
        
        $avatar_str = mb_substr($user_data['avatar'],0,4);

        if($avatar_str != 'http'){
            $user_data['avatar'] = env('OSS_URL').$user_data['avatar'];
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$user_data]);

    }

    /**
     * Get a JWT via given credentials.
     *  统计
     * @return \Illuminate\Http\JsonResponse
     */
    public function Statistics(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $year = $request->post('year');
        if(empty($year)){
            return response()->json(['code' => '203', 'msg' => '请选择年份']);
        }

        $month = $request->post('month');
        if(empty($month)){
            return response()->json(['code' => '203', 'msg' => '请选择月份']);
        }

        $data['target'] = 10000;
        $data['stay'] = 2000;
        $data['sold'] = 8000;

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

}
